package com.ruoyi.baoziyi.service.imp;

import com.ruoyi.baoziyi.domain.SelectBZYImgInfo;
import com.ruoyi.baoziyi.domain.SerBaoziyiDeviceInfo;
import com.ruoyi.baoziyi.domain.SerBaoziyiImgInfo;
import com.ruoyi.baoziyi.mapper.SerBaoziyiDeviceInfoMapper;
import com.ruoyi.baoziyi.mapper.SerBaoziyiImgInfoMapper;
import com.ruoyi.baoziyi.service.BZYImgService;
import com.ruoyi.baoziyi.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class BZYImgServiceImpl implements BZYImgService {
    @Autowired
    private SerBaoziyiDeviceInfoMapper baoziyiDeviceInfoMapper;

    @Autowired
    private SerBaoziyiImgInfoMapper baoziyiImgInfoMapper;

    @Value("${ruoyi.profile}")
    private String profile;

    @Value("${ruoyi.BZYProfile}")
    private String folder_path;
    private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static final SimpleDateFormat SDF1 = new SimpleDateFormat("yyyyMMddHHmmss");
//    private static String folder_path = "E:\\temp\\zhibao\\Spore_pic";
//    private static String folder_path = "/home/xingyou_ftp/zhibao/Spore_pic";
    @Override
    public int getBZYImageInfo() throws ParseException {
        List<SerBaoziyiDeviceInfo> BZYDeviceList = baoziyiDeviceInfoMapper.selectAllDevice();
        int i = 0;
        for(SerBaoziyiDeviceInfo BZYDevice: BZYDeviceList){
            String deviceId = BZYDevice.getDeviceId();
            String img_path = folder_path + "/" + deviceId;
            //调用方法来列出文件夹中所有文件名称
            List<String> imgNameList = FileUtil.listSubfolders(img_path);
            //处理每个文件名
            for(String imgName :imgNameList){
//                System.out.println(imgName);
                String photoName = parseTimestampFromFileName(imgName);
                Date photoDate = SDF1.parse(photoName);
                Timestamp photoTime = new Timestamp(photoDate.getTime());
                SerBaoziyiImgInfo serBaoziyiImgInfo = new SerBaoziyiImgInfo().setDeviceId(deviceId).setPhotoName(imgName).setPhotoPath("/BZYProfile" + "/" + deviceId + "/" + imgName)
                        .setPhotoTime(photoTime).setUpdateTime(SDF.format(new Date()));
                if(baoziyiImgInfoMapper.selectByPhotoName(imgName)!=null){
                    i = i + baoziyiImgInfoMapper.updateByPrimaryKeySelective(serBaoziyiImgInfo);
                }else {
                    i = i + baoziyiImgInfoMapper.insert(serBaoziyiImgInfo);
                }
            }
        }
        return i;
    }

    @Override
    public List<SerBaoziyiImgInfo> selectBZYPhotoList(SelectBZYImgInfo selectImgInfo){
        if(selectImgInfo.getEndTime()!=null){
            Calendar now = Calendar.getInstance();
            now.setTime(selectImgInfo.getEndTime());
            now.set(Calendar.DATE, now.get(Calendar.DATE) + 1);
            selectImgInfo.setEndTime(now.getTime());
        }
        return baoziyiImgInfoMapper.selectBZYPhotoList(selectImgInfo);
    }

    public static String parseTimestampFromFileName(String fileName) {
        Pattern pattern = Pattern.compile("ID_(\\d{14})_\\d+\\.jpg");
        Matcher matcher = pattern.matcher(fileName);

        if (matcher.find()) {
            return matcher.group(1);
        } else {
            return null;
        }
    }

    @Override
    public int deletePhotoById(long id){
        SerBaoziyiImgInfo selectBZYImgInfo = baoziyiImgInfoMapper.selectByPrimaryKey(id);
        String photoPath = selectBZYImgInfo.getPhotoPath();
        File file = new File(photoPath);
        file.delete();
        return baoziyiImgInfoMapper.deleteByPrimaryKey(id);
    }
}
